<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="utils.PageUtil"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="dao.*"%>
<%@page import="pojo.*"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'doAddNews.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<%
		Map<String, Object> map = new HashMap<String, Object>();
		FileItemFactory df = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(df);
		upload.setHeaderEncoding("utf-8");
		List<FileItem> list = upload.parseRequest(request);
		for (FileItem fi : list) {
			if (fi.isFormField()) {
				map.put(fi.getFieldName(), fi.getString("utf-8"));
			} else if (!fi.isFormField()) {
				if (fi.getName() != null && !fi.getName().equals("")) {
					String picName = fi.getName();
					out.print(picName);
					String houzhui = picName.substring(picName.lastIndexOf("."));
					String newPicName = UUID.randomUUID() + houzhui;
					String filepath = application.getRealPath("IMG");
					map.put("npicPath", "IMG/" + newPicName);
					fi.write(new java.io.File(filepath, newPicName));
				} else {
					map.put("npicPath", "");
				}
			}
		}
		if (NewsDao.addNews(new News((String) (map.get("ntitle")), (String) (map.get("nauthor")),
				(String) (map.get("npicPath")), (String) (map.get("ncontent")), (String) (map.get("nsummary")),
				new Date(), new Date(), Integer.parseInt((String) (map.get("ntid")))))) {
	%>
	<script type="text/javascript">
		alert("新增新闻成功");
		location = "JSP/admin.jsp";
	</script>
	<%
		} else {
	%><script type="text/javascript">
		alert("新增新闻失败失败");
		location = "JSP/admin.jsp";
	</script>
	<%
		}
	%>
</body>
</html>
